<?php namespace App\Http\Controllers\Api;

use App\Files;
use App\Http\Controllers\Controller;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Images;
use App\User;
use App\Userinfo;
use App\Videos;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request){
        $uploadFile = $request->file('photo');

        if (!$request->hasFile('photo'))
            return response()->json(['level'=>'error','message'=>'文件不存在！']);

        if (!$uploadFile->isValid())
            return response()->json(['level'=>'error','message'=>'上传失败！']);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::UploadFile($uploadFile, $fileName,"oss");

        $imageModel = new Images();
        $imageModel->file_id = $fileModel->id;
        $imageModel->img_name = $fileName;
        $imageModel->img_type = $fileModel->file_type;
        $imageModel->save();

        $imageModel->img_url = FileManager::getImgFromOssUrl($imageModel->id);

        return response()->json(['level'=>'success','message'=>'上传成功！','data'=>$imageModel]);
    }

    public function uploadVideo(Request $request){
        $uploadFile = $request->file('video');

        if (!$request->hasFile('video'))
            return response()->json(['level'=>'error','message'=>'文件不存在！']);

        if (!$uploadFile->isValid())
            return response()->json(['level'=>'error','message'=>'上传失败！']);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::UploadFile($uploadFile, $fileName,"oss");

        $theVideo = new Videos();
        $theVideo->file_id = $fileModel->id;
        $theVideo->key     = $fileModel->file_md5;
        $theVideo->video_name = $fileModel->file_name;
        $theVideo->video_type = $fileModel->file_type;
        $theVideo->save();

        $theVideo->video_url = FileManager::getVideoFromOssUrl($theVideo->id);

        return response()->json(['level'=>'success','message'=>'上传成功！','data'=>$theVideo]);
    }

    public function showImageById($id=0){
        if(is_numeric($id) && $id == 0)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        /** @var Images $images */
        $images = Images::whereId($id)->first();

        if(!$images)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        $imageFile = $images->files;
        if(!$imageFile || $imageFile == null)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        return \Response::download($imageFile->getLocalPath(),$imageFile->img_name,[],'inline');
    }

    public function uploadUserPhoto(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        if(!$user)
            return response()->json(["level"=>"error","message"=>"no_login","data"=>"no_login"]);

        /** @var Userinfo $userInfo */
        $userInfo = Userinfo::whereUserId($user->id)->first();

        if(!$userInfo)
            return response()->json(["level"=>"error","message"=>"用户信息不存在","data"=>""]);

        $uploadFile = $request->file('photo');

        if(!$request->hasFile('photo'))
            return response()->json(['level'=>'error','message'=>'图片不存在']);

        if(!$uploadFile->isValid())
            return response()->json(['level'=>'error','message'=>'图片上传失败']);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::UploadFile($uploadFile,$fileName,'local');
        /** @var Images $imageModel */
        $imageModel = FileManager::buildImageMultipx($fileModel);

        $userInfo->photo_id = $imageModel->id;
        $userInfo->save();

        return response()->json(['level'=>'success','message'=>'头像上传成功','data'=>(int)$imageModel->id]);
    }
}